1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 
12 module hip.api.renderer.texture;
13 
14 public import hip.api.data.image;
15 public import hip.api.graphics.color;
16 
17 enum TextureWrapMode
18 {
19     CLAMP_TO_EDGE,
20     CLAMP_TO_BORDER,
21     REPEAT,
22     MIRRORED_REPEAT,
23     MIRRORED_CLAMP_TO_EDGE,
24     UNKNOWN
25 }
26 
27 enum TextureFilter
28 {
29     LINEAR,
30     NEAREST,
31     NEAREST_MIPMAP_NEAREST,
32     LINEAR_MIPMAP_NEAREST,
33     NEAREST_MIPMAP_LINEAR,
34     LINEAR_MIPMAP_LINEAR
35 }
36 
37 interface IHipTexture
38 {
39     void setWrapMode(TextureWrapMode mode);
40     void setTextureFilter(TextureFilter min, TextureFilter mag);
41     IHipTexture getBackendHandle();
42 
43     protected bool loadImpl(in IImage img);
44     final bool load(in IImage img)
45     {
46         if(img.hasLoadedData)
47             return loadImpl(img);
48         return false;
49     }
50     void bind(int slot = 0);
51     void unbind(int slot = 0);
52 
53     bool hasSuccessfullyLoaded();
54 
55     int getWidth() const;
56     int getHeight() const;
57 }
58 
59 
60 struct TextureCoordinatesQuad
61 {
62     float u1, v1, u2, v2;
63 }
64 
65 interface IHipTexturizable
66 {
67     void setTexture(IHipTexture texture);
68 }
69 
70 interface IHipTextureRegion
71 {
72     void setTexture(IHipTexture texture);
73     const(IHipTexture) getTexture() const;
74     IHipTexture getTexture();
75     ///Returns this region width
76     int getWidth() const;
77     ///Returns this region height
78     int getHeight() const;
79 
80     void setRegion(float u1, float v1, float u2, float v2);
81     TextureCoordinatesQuad getRegion() const;
82     ref float[8] getVertices();
83 
84     void setFlippedX(bool flip);
85     void setFlippedY(bool flip);
86     bool isFlippedX();
87     bool isFlippedY();
88 
89     /**
90     *   The uint variant from the setRegion receives arguments in a non normalized way to setup
91     *   the UV coordinates.
92     *   It is better if you wish to just pass where it start and ends.
93     *   The region is divided by the width and height
94     */
95     final void setRegion(int width, int height, uint u1, uint v1, uint u2, uint v2)
96     {
97         float fu1 = u1/cast(float)width;
98         float fu2 = u2/cast(float)width;
99         float fv1 = v1/cast(float)height;
100         float fv2 = v2/cast(float)height;
101         setRegion(fu1, fv1, fu2, fv2);
102     }
103 
104     /**
105     *   The UV coordinates passed are divided by the current texture width and height
106     */
107     final void setRegion(uint u1, uint v1, uint u2, uint v2)
108     {
109         setRegion(getTextureWidth(), getTextureHeight(), u1, v1, u2, v2);
110     }
111 
112 
113 
114     final void setTexture(IHipTexture texture, float u1, float v1, float u2, float v2)
115     {
116         setTexture(texture);
117         setRegion(u1,v1,u2,v2);
118     }
119     
120     final int getTextureWidth() const
121     {
122         const IHipTexture tex = getTexture();
123         if(tex)
124             return tex.getWidth();
125         return 0;
126     }
127     final int getTextureHeight() const
128     {
129         const IHipTexture tex = getTexture();
130         if(tex)
131             return tex.getHeight();
132         return 0;
133     }
134 }